import { Injectable, OnModuleInit } from '@nestjs/common';
import { MenuService } from './menu.service';
import { Menu } from './menu.entity';

@Injectable()
export class MenuInitService implements OnModuleInit {
  constructor(private menuService: MenuService) {}

  async onModuleInit() {
    // 检查是否已有菜单数据
    const existingMenus = await this.menuService.findAll();
    if (existingMenus.length === 0) {
      // 初始化示例菜单数据
      await this.initSampleMenus();
    }
  }

  private async initSampleMenus() {
    console.log('初始化示例菜单数据...');
    
    // 创建根菜单
    const dashboardMenu = await this.menuService.create({
      name: '仪表盘',
      path: '/dashboard',
      component: 'Dashboard',
      icon: 'dashboard',
      parentId: 0,
      orderNum: 1,
      status: 1,
      permission: 'menu:dashboard:view',
      type: 1,
    });

    const systemMenu = await this.menuService.create({
      name: '系统管理',
      path: '/system',
      icon: 'setting',
      parentId: 0,
      orderNum: 2,
      status: 1,
      type: 0, // 目录
    });

    const userMenu = await this.menuService.create({
      name: '用户管理',
      path: '/system/user',
      component: 'System/User',
      icon: 'user',
      parentId: systemMenu.id,
      orderNum: 1,
      status: 1,
      permission: 'menu:user:list',
      type: 1,
    });

    const roleMenu = await this.menuService.create({
      name: '角色管理',
      path: '/system/role',
      component: 'System/Role',
      icon: 'team',
      parentId: systemMenu.id,
      orderNum: 2,
      status: 1,
      permission: 'menu:role:list',
      type: 1,
    });

    const menuMenu = await this.menuService.create({
      name: '菜单管理',
      path: '/system/menu',
      component: 'System/Menu',
      icon: 'menu',
      parentId: systemMenu.id,
      orderNum: 3,
      status: 1,
      permission: 'menu:menu:list',
      type: 1,
    });

    console.log('示例菜单数据初始化完成');
  }
}